from rest_framework import serializers

from P3_02 import models


class NavSer(serializers.ModelSerializer):
    class Meta:
        model = models.Nav
        fields = ["id","name","url"]
class CategorySer(serializers.ModelSerializer):
    children = serializers.SerializerMethodField()
    class Meta:
        model = models.Category
        fields = "__all__"
    def get_children(self,obj):
        child = models.Category.objects.filter(parent=obj.id)
        if child:
            return CategorySer(child,many=True).data
        else:
            return []